package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Menu;
import cn.wolfcode.crm.query.MenuQueryObject;
import cn.wolfcode.crm.service.IMenuService;
import cn.wolfcode.crm.util.JsonResult;
import cn.wolfcode.crm.util.PageResult;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("menu")
public class MenuController {
    @Autowired
    private IMenuService menuService;

    @RequiresPermissions(value = {"menu:view", "菜单列表视图"}, logical = Logical.OR)
    @RequestMapping("view")
    public String view() {
        return "menu/menu";
    }

    @RequiresPermissions(value = {"menu:query", "菜单列表列表"}, logical = Logical.OR)
    @RequestMapping("query")
    @ResponseBody
    public PageResult query(MenuQueryObject qo) {
        return menuService.query(qo);
    }


    @RequiresPermissions(value = {"menu:selectAll", "菜单列表全选"}, logical = Logical.OR)
    @RequestMapping("selectAll")
    @ResponseBody
    public List<Menu> selectAll() {
        return menuService.selectAll();
    }

    @RequiresPermissions(value = {"menu:saveOrUpdate", "菜单列表编辑/新增"}, logical = Logical.OR)
    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(Menu entity) {
        try {
            menuService.saveOrUpdate(entity);
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult().mark("操作失败!原因:" + e.getMessage());
        }
    }

    @RequiresPermissions(value = {"menu:delete", "菜单删除"}, logical = Logical.OR)
    @RequestMapping("delete")
    @ResponseBody
    public JsonResult delete(Long id) {
        try {
            menuService.deleteByPrimaryKey(id);
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult().mark("操作失败!原因:" + e.getMessage());
        }
    }

    @RequiresPermissions(value = {"menu:getRootMenu", "根菜单树"}, logical = Logical.OR)
    @RequestMapping("getRootMenu")
    @ResponseBody
    public List<Menu> getRootMenu() {
        return menuService.getRootMenu();
    }


    @RequestMapping("selectLocation")
    public String getWeather(String location) throws Exception {

        return "weather/selectLoaction";
    }

}
